package com.taptech.doufu.ui.adapter.personalcenter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.util.TextUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.viewholder.BaseHolderView;
import com.taptech.doufu.ui.adapter.BaseListAdapter;
import com.taptech.doufu.bean.BaseBean;
import com.taptech.doufu.bean.HomeTopBean;
import com.taptech.doufu.bean.NovelBean;
import com.taptech.doufu.bean.SweepBean;
import com.taptech.doufu.bean.CommentDataBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.GroupInfoBean;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.PersonalDynamicBean;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.ui.activity.PersonalDynamicActivity;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.util.UIUtil.EnsureListener;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.NetworkImageView;
import com.umeng.analytics.MobclickAgent;

/**
 * <p>
 * Company: DiaoBao
 * </p>
 *
 * @author huangzh
 * @date 2014-3-18
 */
public class PersonalDynamicAdapter extends BaseListAdapter implements HttpResponseListener {

    String userName;
    Bitmap userPortrait;
    Activity activity;
    private int type;

    public PersonalDynamicAdapter(String userName, Bitmap userPortrait,
                                  Activity activity) {
        this.userName = userName;
        this.userPortrait = userPortrait;
        this.activity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.personal_center_adapter_dynamic, null);
            convertView.setClickable(true);
            holder = new ViewHolder();

            holder.userHeader = (NetworkImageView) convertView
                    .findViewById(R.id.personal_center_dynamic_user_portrait);
            holder.userName = (TextView) convertView
                    .findViewById(R.id.personal_center_adapter_news_author);
            holder.action = (TextView) convertView
                    .findViewById(R.id.personal_center_adapter_news_action);
            holder.groupName = (TextView) convertView
                    .findViewById(R.id.personal_center_adapter_news_ref_group);
            holder.creatTime = (TextView) convertView
                    .findViewById(R.id.personal_center_adapter_news_time);

            holder.commentGroup = convertView
                    .findViewById(R.id.personal_center_dynamic_comment_group);
            holder.commentText = (TextView) convertView
                    .findViewById(R.id.personal_center_dynamic_comment_text);
            holder.commentpraise = (TextView) convertView
                    .findViewById(R.id.personal_center_dynamic_comment_praise);
            holder.commentRely = (TextView) convertView
                    .findViewById(R.id.personal_center_dynamic_comment_reply);

            holder.contentGroup = convertView
                    .findViewById(R.id.personal_center_dynamic_content_group);
            holder.contentText = (TextView) convertView
                    .findViewById(R.id.personal_center_dynamic_content_text);
            holder.contentComment = (TextView) convertView
                    .findViewById(R.id.personal_center_dynamic_content_comment);
            holder.contentImage = (ImageView) convertView
                    .findViewById(R.id.personal_center_dynamic_content_imgge);

            holder.sweepGroup = convertView
                    .findViewById(R.id.personal_center_sweep_info);
            holder.sweepTitle = (TextView) convertView
                    .findViewById(R.id.home_item_sweep_title);
            holder.sweepDes = (TextView) convertView
                    .findViewById(R.id.home_item_sweep_des);
            holder.sweepTaglist = (TextView) convertView
                    .findViewById(R.id.home_item_sweep_taglist);
            holder.sweepReadTimeImg = (ImageView) convertView
                    .findViewById(R.id.home_item_sweep_read_time_icon);
            holder.sweepReadTimes = (TextView) convertView
                    .findViewById(R.id.home_item_sweep_read_times);
            holder.flowerCount = (TextView) convertView
                    .findViewById(R.id.home_item_sweep_flower_count);
            holder.commentCount = (TextView) convertView
                    .findViewById(R.id.home_item_comment_count);
            holder.mUserImg = (ImageView) convertView
                    .findViewById(R.id.home_item_sweep_userimg);
            holder.mUserName = (TextView) convertView
                    .findViewById(R.id.home_item_sweep_username);
            holder.mAuthorName = (TextView) convertView
                    .findViewById(R.id.home_item_sweep_author);
            holder.mTitleLevel = (TextView) convertView
                    .findViewById(R.id.home_item_sweep_title_level);
            holder.mCreateTime = (TextView) convertView.findViewById(R.id.home_item_sweep_create_time);

            holder.userName.setText(userName);
            holder.userHeader.setFullScreen(false);
            holder.userHeader.setRound(true);
            if (userPortrait == null) {
                ImageManager.displayImage(holder.userHeader, AccountService.getInstance().getBaseAccount().getUser_head_img(), 0);
            } else {
                holder.userHeader.setImageBitmap(userPortrait);

            }

            convertView.setTag(holder);

        }
        initData(convertView, position);
        return convertView;
    }

    private void initData(View convertView, final int position) {
        try {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            final PersonalDynamicBean data = (PersonalDynamicBean) getDataSource()
                    .get(position);
            if (data != null) {
                type = DiaobaoUtil.String2Int(data.getOp_type());
                HomeTopBean article = null;
                if (activity instanceof PersonalDynamicActivity) {
                    convertView.setOnLongClickListener(new OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            UIUtil.showEnsureDialog(activity, "确定删除",
                                    new EnsureListener() {

                                        @Override
                                        public void sure() {
                                            getDataSource().remove(position);
                                            notifyDataSetChanged();
                                            switch (type) {
                                                case Constant.OP_TYPE_COLLECT:
                                                    PersonalInfoService.getInstance().delCollectes(
                                                            data.getTarget_id() + "", PersonalDynamicAdapter.this);
                                                    break;
                                                case Constant.OP_TYPE_UGC:
                                                    PersonalInfoService.getInstance().delTopic(
                                                            ((HomeTopBean) data.getContent()).getId(), PersonalDynamicAdapter.this);
                                                    break;
                                                case Constant.OP_TYPE_COMMENT:
                                                    PersonalInfoService.getInstance().delComment(
                                                            ((CommentDataBean) data.getContent()).getComment_id(), PersonalDynamicAdapter.this);
                                                    break;
                                                case Constant.OP_TYPE_REPLY:
//												PersonalInfoService.getInstance().delReply(
//														((ReplyDataBean)data.getContent()).getComment_id(), PersonalDynamicAdapter.this);
                                                    break;
                                                case Constant.OBJECT_TYPE_SWEEP:
                                                    MobclickAgent.onEvent(activity, "sweep-remove");
                                                    PersonalInfoService.getInstance().delSweep(data.getSweep().getId(), PersonalDynamicAdapter.this);
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }

                                        @Override
                                        public void cancel() {
                                        }
                                    });

                            return true;
                        }
                    });
                }
                BaseBean target = data.getTarget();
                BaseBean content = data.getContent();
                final PersonalDynamicBean myData = data;
                SweepBean sweepBean = myData.getSweep();
                final NovelBean novelBean = myData.getNovel();
                if (type == Constant.OP_TYPE_COMMENT) {
                    CommentDataBean comment = (CommentDataBean) content;
                    holder.commentGroup.setVisibility(View.VISIBLE);
                    holder.commentText.setText(comment.getComment_content());
                    holder.commentpraise.setText(DiaobaoUtil.String2Int(comment
                            .getLike_times()) + " 赞");
                    holder.commentRely.setText(DiaobaoUtil.String2Int(comment
                            .getReply_times()) + " 回复");
                    holder.action.setText("评论了一篇文章");
                    holder.groupName.setVisibility(View.GONE);
                    holder.creatTime.setText(DiaobaoUtil
                            .seconds2TimeString(Long.parseLong(comment
                                    .getComment_time())));
                    holder.contentComment.setVisibility(View.GONE);
                    article = (HomeTopBean) target;
                    holder.sweepGroup.setVisibility(View.GONE);
                } else if (type == Constant.OP_TYPE_UGC) {
                    holder.sweepGroup.setVisibility(View.GONE);
                    holder.commentGroup.setVisibility(View.GONE);
                    holder.action.setText("发布到");
                    holder.groupName.setVisibility(View.VISIBLE);
                    holder.contentComment.setVisibility(View.VISIBLE);
                    GroupInfoBean group = (GroupInfoBean) target;
                    article = (HomeTopBean) content;
                    if (group != null) {
                        holder.groupName.setTextColor(Color.parseColor("#ff6e70"));
                        holder.groupName.setText(group.getName());

                    }
                    if (article != null) {
                        holder.creatTime.setText(DiaobaoUtil
                                .seconds2TimeString(Long.parseLong(article
                                        .getPublish_time())));
                        holder.contentComment.setText(article.getActionMsg()
                                .getComment_times() + " 评论");
                    }

                } else if (type == Constant.OP_TYPE_COLLECT) {
                    holder.sweepGroup.setVisibility(View.GONE);
                    holder.commentGroup.setVisibility(View.GONE);
                    holder.groupName.setVisibility(View.GONE);
                    holder.contentComment.setVisibility(View.GONE);
                    article = (HomeTopBean) target;
                    if (myData.getTarget_type().equalsIgnoreCase("5")) {
                        if (article.getTopic_type() != null) {
                            if (article.getTopic_type().equals("18")) {
                                holder.action.setText("收藏了 一本小说");
                            } else if (article.getTopic_type().equals("16")) {
                                holder.action.setText("收藏了 一篇话题");
                            }
                        }
                    } else if (myData.getTarget_type().equalsIgnoreCase("1")) {
                        holder.action.setText("收藏了 一篇文章");
                    } else if (myData.getTarget_type().equalsIgnoreCase("3")) {
                        holder.action.setText("收藏了 一个视频");
                    }
                } else if (type == Constant.OBJECT_TYPE_SWEEP) {
                    if (sweepBean != null && novelBean != null) {
                        holder.creatTime.setVisibility(View.GONE);
                        holder.groupName.setVisibility(View.GONE);
                        holder.commentGroup.setVisibility(View.GONE);
                        holder.contentGroup.setVisibility(View.GONE);
                        holder.sweepGroup.setVisibility(View.VISIBLE);
                        holder.sweepGroup.setBackgroundColor(Color.parseColor("#ffffff"));
                        holder.action.setText("发表了 一篇文评");
                        holder.sweepTitle.setText(novelBean.getTitle());
                        holder.mAuthorName.setText("作者：" + novelBean.getAuthor());
                        holder.flowerCount.setText(sweepBean.getFlower_num());
                        holder.commentCount.setText(sweepBean.getComment_times());
                        holder.mAuthorName.setVisibility(View.GONE);
                        final String novelId = novelBean.getId();
                        convertView.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
								/*Intent intent = new Intent(v.getContext(), SweepDetailActivity.class);
								intent.putExtra(Constant.ARTICLE_NOVELID, novelId);
								intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								v.getContext().startActivity(intent);*/
                                SimpleWeexActivity.startActivity(v.getContext(), "vue/articalComment/TFArticalComment.js?novelId=" + novelBean.getId());
                            }
                        });
                        //文评信息
                        holder.mUserImg.setVisibility(View.GONE);
                        holder.mUserName.setVisibility(View.GONE);
//						ImageManager.displayImage(holder.mUserImg, userBean.getIcon(),0);
//						holder.mUserName.setText(userBean.getName());
                        holder.sweepTaglist.setText(sweepBean.getTag_list());
                        if (sweepBean.getReview() != null) {
                            TextUtil.handleTextView(holder.sweepDes, sweepBean.getReview(), 5);
                        }
                        if (sweepBean.getStar() != null) {
                            UIUtil.showStarLevel(Integer.valueOf(sweepBean.getStar()), holder.mTitleLevel);
                        }
                        //浏览次数
                        if (sweepBean.getRead_num() != null) {
                            if (sweepBean.getRead_num().equals("0")) {
                                holder.sweepReadTimes.setVisibility(View.GONE);
                                holder.sweepReadTimeImg.setVisibility(View.GONE);
                            } else {
                                holder.sweepReadTimes.setVisibility(View.VISIBLE);
                                holder.sweepReadTimeImg.setVisibility(View.VISIBLE);
                                holder.sweepReadTimes.setText(sweepBean.getRead_num());
                            }
                        }
                        if (sweepBean != null && sweepBean.getAdd_time() != null) {
                            holder.mCreateTime.setText(DiaobaoUtil.seconds2TimeString(Long.parseLong(sweepBean.getAdd_time())));
                        }
                    }
                }

                if (article != null) {
                    holder.contentGroup.setVisibility(View.VISIBLE);
                    holder.contentText.setVisibility(View.VISIBLE);
                    final HomeTopBean topBean = article;
                    holder.contentGroup.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            viewDetails(topBean);
                        }
                    });
                    if (activity instanceof PersonalDynamicActivity) {
                        holder.contentGroup.setOnLongClickListener(new OnLongClickListener() {

                            @Override
                            public boolean onLongClick(View v) {
                                UIUtil.showEnsureDialog(activity, "确定删除",
                                        new EnsureListener() {

                                            @Override
                                            public void sure() {
                                                getDataSource().remove(position);
                                                notifyDataSetChanged();
                                                switch (type) {
                                                    case Constant.OP_TYPE_COLLECT:
                                                        PersonalInfoService.getInstance().delCollectes(
                                                                data.getTarget_id() + "", PersonalDynamicAdapter.this);
                                                        break;
                                                    case Constant.OP_TYPE_UGC:
                                                        PersonalInfoService.getInstance().delTopic(((HomeTopBean) data.getContent()).getId() + "", PersonalDynamicAdapter.this);
                                                        break;
                                                    case Constant.OP_TYPE_COMMENT:
                                                        PersonalInfoService.getInstance().delComment(
                                                                ((CommentDataBean) data.getContent()).getComment_id(), PersonalDynamicAdapter.this);
                                                        break;
                                                    case Constant.OP_TYPE_REPLY:
//													PersonalInfoService.getInstance().delReply(
//															((ReplyDataBean)data.getContent()).getComment_id(), PersonalDynamicAdapter.this);
                                                        break;
                                                    case Constant.OBJECT_TYPE_SWEEP:
                                                        MobclickAgent.onEvent(activity, "sweep-remove");
                                                        PersonalInfoService.getInstance().delSweep(data.getSweep().getId(), PersonalDynamicAdapter.this);
                                                        break;
                                                    default:
                                                        break;
                                                }
                                            }

                                            @Override
                                            public void cancel() {
                                            }
                                        });

                                return true;
                            }
                        });
                    }
                    if (article.getContent() != null && !article.getContent().equals("")) {
                        holder.contentText.setText(article.getContent());
                    } else if (article.getTitle() != null && !article.getTitle().equals("")) {
                        holder.contentText.setText(article.getTitle());
                    } else {
                        holder.contentText.setVisibility(View.GONE);
                    }
                    if (article.getImages() != null && article.getImages().length != 0) {
                        holder.contentImage.setVisibility(View.VISIBLE);
                        holder.contentImage.setImageResource(R.drawable.bg_home_object_default);
                        ImageManager.displayImage(holder.contentImage, article.getImages()[0].getImgUrl(), 0);
                    } else {
                        holder.contentImage.setVisibility(View.GONE);
                    }
                } else {
                    holder.contentGroup.setVisibility(View.GONE);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void viewDetails(HomeTopBean topBean) {
        DiaobaoUtil.contentClick(activity, topBean);
    }

    protected class ViewHolder extends BaseHolderView {
        NetworkImageView userHeader;
        ImageView contentImage;
        TextView userName, groupName, action, creatTime, commentText,
                commentpraise, commentRely, contentText, contentComment;
        View contentGroup, commentGroup, sweepGroup;

        TextView mTitleLevel;
        TextView sweepTitle;
        TextView sweepDes;
        TextView sweepTaglist;
        TextView sweepReadTimes;
        TextView mUserName;
        TextView mAuthorName;
        TextView flowerCount;
        TextView commentCount;
        ImageView sweepReadTimeImg;
        ImageView mUserImg;
        TextView mCreateTime;
    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        // TODO Auto-generated method stub

    }

}
